
package com.gzhryc.shared_device.oem.h5.mng.controllers.member;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.config.PropertyTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.json.JsonTools;
import com.gzhryc.jdbc.DateBranchHelper;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.base.enums.EOperatorType;
import com.gzhryc.shared_device.base.models.OperateInfo;
import com.gzhryc.shared_device.base.models.OrderLogContent;
import com.gzhryc.shared_device.common.payment.dao.db.WXTransferMoneyOrder;
import com.gzhryc.shared_device.common.payment.services.WXTransferMoneyOrderService;
import com.gzhryc.shared_device.oem.code.PayServiceFactory;
import com.gzhryc.shared_device.oem.code.member.dao.db.MemberRechargeRefundOrder;
import com.gzhryc.shared_device.oem.code.member.dao.db.MemberTransferMoneyOrder;
import com.gzhryc.shared_device.oem.code.member.dao.db.MemberTransferMoneyOrderLog;
import com.gzhryc.shared_device.oem.code.member.dao.enums.MemberTransferMoneyOrderEnum;
import com.gzhryc.shared_device.oem.code.member.services.MemberRechargeRefundOrderService;
import com.gzhryc.shared_device.oem.code.member.services.MemberTransferMoneyOrderLogService;
import com.gzhryc.shared_device.oem.code.member.services.MemberTransferMoneyOrderService;
import com.gzhryc.shared_device.oem.code.member.services.dto.MemberTransferMoneyOrderSearch;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;
import com.gzhryc.system.SysConstants;
import com.gzhryc.system.dao.db.Manager;

public class MemberTransferMoneyOrderController {

    @Route(value = { "/member/transfer_money_order/list.html" })
    public String list(HttpServletRequest request) {
        request.setAttribute("stateList", MemberTransferMoneyOrderEnum.State.values());
        request.setAttribute("handleTypeList",MemberTransferMoneyOrderEnum.HandleType.values());
        request.setAttribute("dateBranchItemList", DateBranchHelper.self().getYearDateBranchItems());
        return "/member/transfer_money_order/list.html";
    }

    @Route(value = "/member/transfer_money_order/list.data")
    public ListResult<MemberTransferMoneyOrder> listData(HttpServletRequest request) {
        ListResult<MemberTransferMoneyOrder> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        MemberTransferMoneyOrderSearch search = WebUtils.getParameter(request, MemberTransferMoneyOrderSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);
        
        Date dateBranch = new Date();
        if(StringTools.isNotBlank(search.getDateBranchId())){
            dateBranch = DateBranchHelper.getDateByItemId(search.getDateBranchId());
        }

        result.getBody().setTotal(MemberTransferMoneyOrderService.self(dateBranch).countBySearch(search));
        List<MemberTransferMoneyOrder> memberList = MemberTransferMoneyOrderService.self(dateBranch).findBySearch(search,pageNo,pageLimit);
        if(memberList != null && memberList.size() > 0){
            result.setCode(DtoConstants.SUCCESS,null);
            result.getBody().setData(memberList);
        }
        return result;
    }

    @Route(value = { "/member/transfer_money_order/info.html" })
    public String info(HttpServletRequest request) {
        String tradeNo = WebUtils.getString(request,"tradeNo");
        Date createDate = WebUtils.getDate(request,"createDate");
        if(createDate == null){
            createDate = new Date();
        }
        MemberTransferMoneyOrder order = MemberTransferMoneyOrderService.self(createDate).getByTradeNo(tradeNo);
        if(order != null) {
            WebUtils.setAttribute(request, order);
            
            List<MemberRechargeRefundOrder> refundOrderList = MemberRechargeRefundOrderService.self(createDate).findByTransferTradeNo(order.getTradeNo());
            if(refundOrderList != null && refundOrderList.size() > 0) {
            	 request.setAttribute("refundOrderList", refundOrderList);
            }
        }
        return "/member/transfer_money_order/info.html";
    }

    @Route(value = { "/member/transfer_money_order/handle.html" })
    public String handle(HttpServletRequest request) {
        String tradeNo = WebUtils.getString(request,"tradeNo");
        Date createDate = WebUtils.getDate(request,"createDate");
        if(createDate == null){
            createDate = new Date();
        }
        MemberTransferMoneyOrder order = MemberTransferMoneyOrderService.self(createDate).getByTradeNo(tradeNo);
        if(order != null) {
            WebUtils.setAttribute(request, order);
            WXTransferMoneyOrder wxTransferMoneyOrder = WXTransferMoneyOrderService.self().getByTradeNo(order.getTradeNo());
            if(wxTransferMoneyOrder != null) {
                if(StringTools.isNotBlank(wxTransferMoneyOrder.getFaceImg())){
                    String resDomain = PropertyTools.get().getString("resDomain");
                    wxTransferMoneyOrder.setFaceImg(WebUtils.setHttpDomain(resDomain,wxTransferMoneyOrder.getFaceImg()));
                }
                request.setAttribute("transferMoneyOrder", wxTransferMoneyOrder);
            }
        }
        return "/member/transfer_money_order/handle.html";
    }

    @Route(value = { "/member/transfer_money_order/handle.do" })
    public AjaxResult doHandle(HttpServletRequest request, HttpSession session) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"处理失败，请稍后再试");
        Manager manager = WebUtils.getAttribute(session, SysConstants.SESSION_LOGIN_MANAGER, Manager.class);
        if (manager == null) {
            result.setCode(DtoConstants.LOGIN_OVERDUE, "登录过期，请重新登录");
            return result;
        }
        String tradeNo = WebUtils.getString(request,"tradeNo");
        Date createDate = WebUtils.getDate(request,"createDate");
        if(createDate == null){
            createDate = new Date();
        }
        Integer handleType = WebUtils.getInteger(request,"handleType");
        String operateNote = WebUtils.getString(request,"operateNote");
        if(StringTools.isNotBlank(tradeNo) && handleType != null && StringTools.isNotBlank(operateNote)) {
            OperateInfo operateInfo = new OperateInfo(WebConstants.PROGRAM_TYPE,WebConstants.PROGRAM_TYPE_NAME);
            operateInfo.setOperator( EOperatorType.Manager.index(),manager.getFullName());
            operateInfo.setOperateNote(operateNote);

            try {
                if (MemberTransferMoneyOrderService.self(createDate).manualProcessing(tradeNo,handleType,PayServiceFactory.self(),operateInfo)) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }

    @Route(value = { "/member/transfer_money_order/reject.html" })
    public String reject(HttpServletRequest request) {
        return "/member/transfer_money_order/reject.html";
    }

    /**
     * 驳回
     * @param request
     * @return
     */
    @Route(value = { "/member/transfer_money_order/reject.do" })
    public AjaxResult doReject(HttpServletRequest request, HttpSession session) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"处理失败，请稍后再试");
        Manager manager = WebUtils.getAttribute(session, SysConstants.SESSION_LOGIN_MANAGER, Manager.class);
        if (manager == null) {
            result.setCode(DtoConstants.LOGIN_OVERDUE, "登录过期，请重新登录");
            return result;
        }
        String tradeNo = WebUtils.getString(request,"tradeNo");
        Date createDate = WebUtils.getDate(request,"createDate");
        if(createDate == null){
            createDate = new Date();
        }
        String rejectNote = WebUtils.getString(request,"rejectNote");
        if(StringTools.isNotBlank(tradeNo) && StringTools.isNotBlank(rejectNote)) {
            OperateInfo operateInfo = new OperateInfo(WebConstants.PROGRAM_TYPE,WebConstants.PROGRAM_TYPE_NAME);
            operateInfo.setOperator( EOperatorType.Manager.index(),manager.getFullName());
            operateInfo.setOperateNote("驳回申请，原因：" + rejectNote);
            try {
                if (MemberTransferMoneyOrderService.self(createDate).reject(tradeNo,rejectNote,operateInfo)) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }

    @Route(value = "/member/transfer_money_order/operate/list.data")
    public ListResult<OrderLogContent.LogItem> operateListData(HttpServletRequest request) {
        ListResult<OrderLogContent.LogItem> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        String tradeNo = WebUtils.getString(request,"tradeNo");
        MemberTransferMoneyOrderLog orderLog = MemberTransferMoneyOrderLogService.self().getByTradeNo(tradeNo);
        if(orderLog != null  && StringTools.isNotBlank(orderLog.getContent())){
            OrderLogContent orderLogContent = JsonTools.fromJson(orderLog.getContent(),OrderLogContent.class);
            if(orderLogContent != null && orderLogContent.getItems() != null && orderLogContent.getItems().size() > 0){
                result.setCode(DtoConstants.SUCCESS,null);
                result.getBody().setData(orderLogContent.getItems());
            }
        }
        return result;
    }
}
